//
//  KLDetailBillTableViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/6/23.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLDetailBillTableViewController.h"
#import "KLHelp.h"
#import "Masonry.h"
#import "UIView+i7Rotate360.h"
#import "REMenu.h"
#import "KLHttpAPI.h"

@interface KLDetailBillTableViewController ()<REMenuDelegate>
{
    UILabel *titleLabel;
    UIImageView *titleImage;
    NSInteger i;
}
@property (strong,nonatomic) REMenu *menu;
@property (strong,nonatomic) NSArray *allList;
@property (strong,nonatomic) NSMutableArray *zhaunruARR;
@property (strong,nonatomic) NSMutableArray *zhuanchuARR;
@property (strong,nonatomic) NSArray *tempShowARR;
@end

@implementation KLDetailBillTableViewController
-(NSArray *)allList{
    if(!_allList){
        _allList = [NSArray array];
    }
    return _allList;
}

-(NSMutableArray *)zhaunruARR{
    if(!_zhaunruARR){
        _zhaunruARR = [NSMutableArray array];
    }
    return _zhaunruARR;
}

-(NSMutableArray *)zhuanchuARR{
    if(!_zhuanchuARR){
        _zhuanchuARR = [NSMutableArray array];
    }
    return _zhuanchuARR;
}

-(NSArray *)tempShowARR{
    if(!_tempShowARR){
        _tempShowARR = [[NSArray alloc]init];
    }
    
    return _tempShowARR;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    i = 1;
    //初始化导航栏
    [self naveSet];
    //创建弹出菜单
    [self creatREmenue];
    [KLHttpAPI requestAccounListWithCompletionHandler:^(MKNetworkOperation *operation) {
        NSDictionary *dic = operation.responseJSON;
        NSString *isSuccess = [dic objectForKey:@"success"];
        if(isSuccess.integerValue == 1){
            NSDictionary *infor = [dic objectForKey:@"infor"];
            self.allList = [infor objectForKey:@"listItems"];
            self.tempShowARR = [self.allList copy];
            [self.tableView reloadData];
            NSLog(@"listItems:%@",self.allList);
            
            if(![self.allList isKindOfClass:[NSNull class]]){
                for (NSDictionary *detailAccount in self.allList) {
                    NSString *keytype = [detailAccount objectForKey:@"keytype"];
                    if(keytype.integerValue == 1 || keytype.integerValue == 4){//转入
                        [self.zhaunruARR addObject:detailAccount];
                    }else{//转出
                        [self.zhuanchuARR addObject:detailAccount];
                    }
                }
            }else{
                [KLHelp showTextHUDforView:self.view text:@"您没有最近一个月的明细,历史明细可点击右上角查询"];
            }
            
        }
    } errorHandler:^(NSString *error) {
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if([self.tempShowARR isKindOfClass:[NSNull class]]){
        return 0;
    }else{
        return self.tempShowARR.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailBill"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detailBill"];
    }else{
        //删除cell的所有子视图
        while ([cell.contentView.subviews lastObject] != nil){
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    NSDictionary *currentDic = self.tempShowARR[indexPath.row];
    
    UILabel *name = [[UILabel alloc]init];
    name.font = [UIFont systemFontOfSize:16];
    [cell.contentView addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15);
    }];
    
    UILabel *balance = [[UILabel alloc]init];
    balance.text = [NSString stringWithFormat:@"余额：%@",[currentDic objectForKey:@"balance"]];
    balance.font = [UIFont systemFontOfSize:13];
    [cell.contentView addSubview:balance];
    [balance mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.bottom.mas_equalTo(-15);
    }];
    
    UILabel *time = [[UILabel alloc]init];
    time.font = [UIFont systemFontOfSize:12];
    time.text = [currentDic objectForKey:@"regdate"];
    time.textColor = [UIColor grayColor];
    time.alpha = 0.5;
    [cell.contentView addSubview:time];
    [time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.right.mas_equalTo(-15);
    }];
    
    UILabel *amount = [[UILabel alloc]init];
    amount.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [cell.contentView addSubview:amount];
    [amount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-15);
    }];
    
    if([[currentDic objectForKey:@"keytype"] isEqualToString:@"1"]){
        name.text = @"充值";
        amount.text = [NSString stringWithFormat:@"+%@",[currentDic objectForKey:@"amount"]];
    }else if([[currentDic objectForKey:@"keytype"] isEqualToString:@"2"]){
        name.text = @"在线支付";
        amount.text = [currentDic objectForKey:@"amount"];
    }else if([[currentDic objectForKey:@"keytype"] isEqualToString:@"3"]){
        name.text = @"提现";
        amount.text = [currentDic objectForKey:@"amount"];
    }else if([[currentDic objectForKey:@"keytype"] isEqualToString:@"4"]){
        name.text = @"收入";
        amount.text = [NSString stringWithFormat:@"+%@",[currentDic objectForKey:@"amount"]];
    }else if([[currentDic objectForKey:@"keytype"] isEqualToString:@"5"]){
        name.text = @"退款";
        amount.text = [NSString stringWithFormat:@"+%@",[currentDic objectForKey:@"amount"]];
    }
    
//    cell.accessoryType = UITableViewCellAccessoryDetailButton;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 68.0f;
}

#pragma mark - 初始化导航栏
-(void)naveSet{
    self.navigationController.navigationBarHidden = NO;
    [KLHelp navSet:self doBackHandeler:^(UIButton *backButton) {
        [backButton addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    }];
    
    //导航栏标题视图
    UIButton *titleView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 135, 30)];
    titleView.backgroundColor = [UIColor colorWithRed:(float)8/255 green:(float)128/255 blue:(float)181/255 alpha:1];
    [titleView addTarget:self action:@selector(orderBill:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = titleView;
    //标题文字
    titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"金额明细";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    [titleView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.equalTo(titleView).with.offset(0);
    }];
    //标题图片
    titleImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"R小三角下"]];
    [titleView addSubview:titleImage];
    [titleImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleView).with.offset(0);
        make.right.mas_equalTo(-20);
        make.width.mas_equalTo(13);
        make.height.mas_equalTo(8);
    }];
    self.tableView.tableFooterView = [[UIView alloc]init];
}-(void)doBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

//创建REmenue
-(void)creatREmenue{
    REMenuItem *allBillItem = [[REMenuItem alloc] initWithTitle:@"全部"
                                                       subtitle:nil
                                                          image:nil
                                               highlightedImage:nil
                                                         action:^(REMenuItem *item) {
                                                             self.tempShowARR = [self.allList copy];
                                                             titleLabel.text = @"金额明细";
                                                             [self.tableView reloadData];
                                                         }];
    REMenuItem *payBillItem = [[REMenuItem alloc] initWithTitle:@"转出"
                                                       subtitle:nil
                                                          image:nil
                                               highlightedImage:nil
                                                         action:^(REMenuItem *item) {
                                                             self.tempShowARR = [self.zhuanchuARR copy];
                                                             titleLabel.text = @"转出明细";
                                                             [self.tableView reloadData];
                                                             
                                                         }];
    REMenuItem *incomeBillItem = [[REMenuItem alloc] initWithTitle:@"转入"
                                                          subtitle:nil
                                                             image:nil
                                                  highlightedImage:nil
                                                            action:^(REMenuItem *item) {
                                                                self.tempShowARR = [self.zhaunruARR copy];
                                                                titleLabel.text = @"转入明细";
                                                                [self.tableView reloadData];
                                                                
                                                            }];
    allBillItem.tag = 0;
    payBillItem.tag = 1;
    incomeBillItem.tag = 2;
    self.menu = [[REMenu alloc] initWithItems:@[allBillItem, payBillItem, incomeBillItem]];
    self.menu.delegate = self;
}

#pragma mark - 点击了导航栏标题
-(void)orderBill:(id)sender{
    //判断是否推出弹出视图
    if(i % 2 == 0){
        [self.menu close];
        NSLog(@"关闭menue");
    }else{
        [self.menu showFromNavigationController:self.navigationController];
        NSLog(@"打开menue");
    }
}

#pragma mark - REMenuDelegate
-(void)willCloseMenu:(REMenu *)menu{
    [UIView animateWithDuration:0.3 animations:^{
        titleImage.transform = CGAffineTransformMakeRotation((180.0f * M_PI * (i%2)) / 180.0f);
        i++;
    }];
}

-(void)willOpenMenu:(REMenu *)menu{
    [UIView animateWithDuration:0.3 animations:^{
        titleImage.transform = CGAffineTransformMakeRotation((180.0f * M_PI * (i%2)) / 180.0f);
        i++;
    }];
}

-(void)viewWillDisappear:(BOOL)animated{
    [self.menu close];
}

@end
